package com.firebird.app.kotlin.stock.trend.fragment

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import android.widget.AdapterView.OnItemClickListener
import android.widget.ImageView
import android.widget.ListView
import androidx.fragment.app.Fragment
import com.android.volley.Request
import com.android.volley.Response
import com.firebird.app.kotlin.stock.trend.StockApp
import com.firebird.app.kotlin.stock.trend.adapter.NewsYwListViewAdapter
import com.firebird.app.kotlin.stock.trend.bean.News
import com.firebird.app.kotlin.stock.trend.utils.DensityUtil.dip2px
import com.firebird.app.kotlin.stock.trend.R
import com.firebird.app.kotlin.stock.trend.activity.NewsDetailActivity
import com.firebird.app.kotlin.stock.trend.net.ObjectRequest
import com.firebird.app.kotlin.stock.trend.view.TouTiaoPagerView
import com.google.gson.reflect.TypeToken
import java.util.*


/**
 * 显示资讯窗口中 要闻信息的 Fragment
 * @author ljk
 * @date   2019年11月6日
 */
class FragmentNewsYw : Fragment() {
    private var mContext: Context? = null
    private var pagerView: TouTiaoPagerView? = null
    private var lv_yaowen: ListView? = null
    private var newsList: MutableList<News>? = null
    private var toutiaoList: MutableList<View>? = null
    private var ywAdapter: NewsYwListViewAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = context
        touTiaoData
        getNewsList()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val params = AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT)
        lv_yaowen = ListView(context)
        lv_yaowen!!.layoutParams = params
        pagerView = TouTiaoPagerView(context!!, resources.getLayout(R.layout.layout_news_toutio))
        params.height = dip2px(context!!, 150f)
        pagerView!!.setLayoutParams(params)
        pagerView!!.setPagerList(toutiaoList)
        pagerView!!.setOnPageChangeListener(pagerView)
        lv_yaowen!!.addHeaderView(pagerView)
        ywAdapter = NewsYwListViewAdapter(context!!, newsList)
        lv_yaowen!!.adapter = ywAdapter
        lv_yaowen!!.onItemClickListener = OnItemClickListener { arg0, view, position, id ->
            //Toast.makeText(context, "position "+position+" ; id "+id, Toast.LENGTH_SHORT).show();;
            val intent = Intent(context, NewsDetailActivity::class.java)
            intent.putExtra("news", newsList!![position - 1])
            context!!.startActivity(intent)
        }
        return lv_yaowen!!
    }

    override fun onStart() {
        super.onStart()
    }

    /**
     * 获取新闻列表数据
     */
    private fun getNewsList() { //String url = "http://112.124.121.218:8080/edu/stock/newslist.html";
        val url = context!!.resources.getString(R.string.url_news_yaowen_lv)
        val successListener: Response.Listener<MutableList<News>> = Response.Listener<MutableList<News>> { resultList ->
            newsList = resultList
            ywAdapter!!.setDate(resultList)
            //System.out.println("============== "+resultList.get(0).getTitle());
        }
        val errorListener = Response.ErrorListener { println("============== 失败") }
        val newsRequest: Request<MutableList<News>> = ObjectRequest(Request.Method.GET, object : TypeToken<List<News?>?>() {}.getType(), url, successListener, errorListener)
        StockApp.requestQueue!!.add(newsRequest)
    }

    /**
     * 获取头条新闻数据
     */
    private val touTiaoData: Unit
         get() {
            val para = AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT)
            val img1 = ImageView(context)
            img1.setBackgroundResource(R.mipmap.img_ipo)
            img1.layoutParams = para
            val img2 = ImageView(context)
            img2.setBackgroundColor(Color.BLUE)
            img2.layoutParams = para
            toutiaoList = ArrayList()
            toutiaoList!!.add(img1)
            toutiaoList!!.add(img2)
        }
}